home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 2 / LSD and 17bit Compendium Deluxe - Volume II.iso / a / prog / asmsrc / thesource-7.lha / Source / PushBClass.lha / pushbclass / pushbclass.c next >
C/C++ Source or Header  |  1994-06-11  |  6KB  |  222 lines

  1. /*
  2.  * PushButtonClass
  3.  */
  4.  
  5. #include <string.h>
  6.  
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <exec/nodes.h>
  10. #include <exec/ports.h>
  11. #include <intuition/intuition.h>
  12. #include <intuition/classes.h>
  13. #include <intuition/classusr.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <intuition/imageclass.h>
  16. #include <graphics/gfxbase.h>
  17. #include <graphics/text.h>
  18. #include <utility/tagitem.h>
  19. #include <utility/hooks.h>
  20.  
  21. #include <clib/exec_protos.h>
  22. #include <clib/intuition_protos.h>
  23. #include <clib/graphics_protos.h>
  24. #include <clib/utility_protos.h>
  25. #include <clib/alib_protos.h>
  26. #include <clib/alib_stdio_protos.h>
  27.  
  28. #include "Gadget.h"
  29. #include "PushBClass.h"
  30.  
  31. extern struct GfxBase *GfxBase;
  32.  
  33. /*
  34.  * Local prototypes
  35.  */
  36.  
  37. static BOOL SetPBIText(struct PushBData *, struct TagItem *);
  38. static ULONG PushBDispatch(Class *, Object *, Msg);
  39.  
  40. /*
  41.  * External variables
  42.  */
  43.  
  44. extern Class *PBClass;
  45.  
  46. /*
  47.  * Local variables
  48.  */
  49.  
  50. static struct TagItem tmpTag[] = {
  51.     {GA_IntuiText, NULL},
  52.     {GA_Image, NULL},
  53.     {TAG_MORE, NULL}
  54. };
  55.  
  56. static struct PushBTA pbTA;
  57.  
  58. /*
  59.  * SetPBIText
  60.  */
  61.  
  62. static BOOL SetPBIText(struct PushBData *instData, struct TagItem *tagItems)
  63. {
  64.     register char *text;
  65.     register BYTE upos;
  66.  
  67.     if (text = (char *)GetTagData(PUSHB_Text, 0L, tagItems)) {
  68.  
  69.         /* Setup first intuitext (button text) */
  70.         instData->PBIText1.FrontPen = instData->PBIText2.FrontPen
  71.                 = instData->PBDrawInfo->dri_Pens[DETAILPEN];
  72.         instData->PBIText1.DrawMode = instData->PBIText2.DrawMode
  73.                 = JAM1;
  74.         instData->PBIText1.IText = text;
  75.  
  76.         /* Setup underlined key intuitext */
  77.         instData->PBIText1.NextText = NULL;
  78.     } else {
  79.  
  80.         text = instData->PBIText1.IText;
  81.     }
  82.  
  83.     if (text) {
  84.  
  85.         if ((upos = (BYTE)GetTagData(PUSHB_UPos, 0L, tagItems))
  86.             && upos > 0 && upos <= strlen(text)) {
  87.  
  88.             /* Get character and length */
  89.             instData->PBIText2.IText = &instData->PBKey[0];
  90.             instData->PBIText2.IText[0] = text[upos-1];
  91.             text[upos-1] = 0;
  92.             instData->PBIText2.LeftEdge = IntuiTextLength(&instData->PBIText1);
  93.             text[upos-1] = instData->PBIText2.IText[0];
  94.  
  95.             instData->PBIText1.NextText = &instData->PBIText2;
  96.  
  97.         }
  98.  
  99.         return TRUE;
  100.     }
  101.  
  102.     return FALSE;
  103. }
  104.  
  105. /*
  106.  *  PushBDispatch
  107.  */
  108.  
  109. static ULONG PushBDispatch(Class *class, Object *object, Msg msg)
  110. {
  111.     register ULONG result = 0;
  112.     register struct PushBInst *instData;
  113.     register struct PushBData *pbData;
  114.     struct Gadget *gadg;
  115.     struct Image *img;
  116.  
  117.     switch (msg->MethodID) {
  118.         case OM_NEW:
  119.             if (img = NewObject(NULL, "frameiclass", IA_FrameType, FRAME_BUTTON, TAG_END)) {
  120.  
  121.                 if (pbData = AllocMem(sizeof(struct PushBData), MEMF_CLEAR)) {
  122.  
  123.                     pbData->PBDrawInfo = (struct DrawInfo *)GetTagData(GA_DrawInfo, 0L, ((struct opSet *)msg)->ops_AttrList);
  124.                     pbData->PBIText1.ITextFont = &pbTA.PBTextAttr1;
  125.                     pbData->PBIText2.ITextFont = &pbTA.PBTextAttr2;
  126.                     if (SetPBIText(pbData, ((struct opSet *)msg)->ops_AttrList)) {
  127.  
  128.                         tmpTag[0].ti_Data = (ULONG)(&pbData->PBIText1);
  129.                         tmpTag[1].ti_Data = (ULONG)img;
  130.                         tmpTag[2].ti_Data = (ULONG)((struct opSet *)msg)->ops_AttrList;
  131.  
  132.                         ((struct opSet *)msg)->ops_AttrList = tmpTag;
  133.                         if (result = (ULONG)(gadg = (struct Gadget *)DoSuperMethodA(class, object, msg))) {
  134.  
  135.                             ((struct opSet *)msg)->ops_AttrList = (struct TagItem *)tmpTag[1].ti_Data;
  136.                             instData = INST_DATA(class, gadg);
  137.                             instData->PBData = pbData;
  138.                             break;
  139.                         }
  140.  
  141.                         ((struct opSet *)msg)->ops_AttrList = (struct TagItem *)tmpTag[1].ti_Data;
  142.                     }
  143.  
  144.                     FreeMem(pbData, sizeof(struct PushBData));
  145.                 }
  146.  
  147.                 DisposeObject(img);
  148.                 result = 0;
  149.             }
  150.  
  151.             break;
  152.         case OM_SET:
  153.             instData = INST_DATA(class, object);
  154.             if (SetPBIText(instData->PBData, ((struct opSet *)msg)->ops_AttrList)) {
  155.  
  156.                 result = SetSuperAttrs(class, object,
  157.                                         GA_IntuiText, &instData->PBData->PBIText1,
  158.                                         TAG_END);
  159.             }
  160.  
  161.             result = DoSuperMethodA(class, object, msg);
  162.             break;
  163.  
  164.         case OM_DISPOSE:
  165.             instData = INST_DATA(class, object);
  166.             FreeMem(instData->PBData, sizeof(struct PushBData));
  167.             DisposeObject(((struct Gadget *)object)->GadgetRender);
  168.             /* NOTE: I'm falling through here! */
  169.  
  170.         default:
  171.             result = DoSuperMethodA(class, object, msg);
  172.             break;
  173.     }
  174.  
  175.     return(result);
  176. }
  177.  
  178. /*
  179.  *  MakePushBClass
  180.  */
  181.  
  182. Class *MakePushBClass(void)
  183. {
  184.     Class *class = NULL;
  185.     extern ULONG HookEntry();     /* defined in amiga.lib */
  186.  
  187.     if (class = MakeClass(NULL, "frbuttonclass", NULL,
  188.                             sizeof(struct PushBInst), 0)) {
  189.  
  190.         class->cl_Dispatcher.h_Entry = HookEntry;
  191.         class->cl_Dispatcher.h_SubEntry = PushBDispatch;
  192.  
  193.         pbTA.PBTextAttr1.ta_Name = pbTA.PBTextAttr2.ta_Name
  194.                 = GfxBase->DefaultFont->tf_Message.mn_Node.ln_Name;
  195.         pbTA.PBTextAttr1.ta_YSize = pbTA.PBTextAttr2.ta_YSize
  196.                 = GfxBase->DefaultFont->tf_YSizendow,
  197.                                 struct Requester *req,
  198.                                 struct DrawInfo *drawInfo,
  199.                                 struct Gadget *prevGadg,
  200.                                 WORD left, WORD top,
  201.                                 char *text, BYTE uposht, STDPBHEIGHT,
  202.                                                 TAG_END);
  203.     }
  204.  
  205.     return tmpGadget;
  206. }
  207.  
  208. /*
  209.  * ChangePushButton
  210.  */
  211.  
  212. void ChangePushButton(struct Gadget *gadget,
  213.                         struct Window *window,
  214.                         struct Requester *req,
  215.                         char *text, BYTE upos)
  216. {
  217.     SetGadgetAttrs(gadget, window, req, GA_Width, STDPBWIDTH, GA_Height, STDPBHEIGHT,
  218.                                             PUSHB_Text, text, PUSHB_UPos, upos,
  219.                                             TAG_END);
  220.     RefreshGList(gadget, window, req, 1);
  221. }
  222.